package com.yowasa.xiuxian.engine.message.cmd;

import cn.hutool.core.lang.WeightRandom;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.yowasa.xiuxian.annoations.CMD;
import com.yowasa.xiuxian.engine.message.CmdMessageHandler;
import com.yowasa.xiuxian.entity.enums.CmdScopeEnum;
import com.yowasa.xiuxian.entity.mys.SendMessage;
import com.yowasa.xiuxian.entity.po.XiuXianExtend;
import com.yowasa.xiuxian.service.helper.ExtendHelper;
import com.yowasa.xiuxian.service.helper.MessageHelper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@CMD(name = "/随机", scope = CmdScopeEnum.ALL)
public class RandomHandler extends CmdMessageHandler {

    @Autowired
    private MessageHelper messageHelper;


    @Override
    public void execute(SendMessage message) {
        List<String> params = getAllParam(message);
        List<WeightRandom.WeightObj<String>> list = new ArrayList<WeightRandom.WeightObj<String>>();
        for (String i : params){
            String[] split = i.split("\\*");
            Integer num;
            try {
                num = Integer.parseInt(split[1]);
            }catch (Exception e){
                num = 1;
            }
            list.add(new WeightRandom.WeightObj<>(split[0],num));
        }

        WeightRandom<String> stringWeightRandom = RandomUtil.weightRandom(list);

        messageHelper.replyMys(message, "抽中的是 ："+stringWeightRandom.next());
    }

    public String desc(){
        return new StringBuffer().append("一个加权随机指令，和游戏无关\n")
                .append("使用方法 /随机 名称*权重 名称*权重\n")
                .append("随机的物品数量无上限，权重必须为整数，名称可以是任意字符\n")
                .toString();

    }

}
